home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Resources / Online / Term / Extras / Source / term-source.lha / Flow.c < prev    next >
C/C++ Source or Header  |  1996-10-20  |  9KB  |  420 lines

  1. /*
  2. **    Flow.c
  3. **
  4. **    Data flow scanner routines
  5. **
  6. **    Copyright © 1990-1996 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. **
  9. **    :ts=4
  10. */
  11.  
  12. #ifndef _GLOBAL_H
  13. #include "Global.h"
  14. #endif
  15.  
  16.     /* Hints for the data flow scanner. */
  17.  
  18. STATIC WORD    ScanStart,ScanEnd;
  19.  
  20. STATIC WORD    AttentionCount[SCAN_COUNT],AttentionLength[SCAN_COUNT],FlowCount;
  21.  
  22.     /* FlowFilter(STRPTR Data,LONG Size,UBYTE Mask):
  23.      *
  24.      *    Data flow filter.
  25.      */
  26.  
  27. LONG
  28. FlowFilter(UBYTE *Data,LONG Size,UBYTE Mask)
  29. {
  30.     LONG OriginalSize;
  31.     UBYTE c;
  32.  
  33.         /* We will return the number of bytes in the buffer
  34.          * after processing it.
  35.          */
  36.  
  37.     OriginalSize = Size;
  38.  
  39.         /* Run until done. */
  40.  
  41.     do
  42.     {
  43.         if(c = (*Data++ & Mask))
  44.         {
  45.  
  46.                 /* We already got a `CONNECT';
  47.                  * continue scanning the serial output
  48.                  * data for the actual baud rate.
  49.                  */
  50.  
  51.             if(BaudPending)
  52.             {
  53.                 BOOL GotIt;
  54.  
  55.                     /* Check if we found a line terminator. */
  56.  
  57.                 if(c < ' ')
  58.                     GotIt = TRUE;
  59.                 else
  60.                 {
  61.                     GotIt = FALSE;
  62.  
  63.                         /* Check if can add another character to the
  64.                          * buffer.
  65.                          */
  66.  
  67.                     if(BaudCount > 0 || c != ' ')
  68.                     {
  69.                         BaudBuffer[BaudCount++] = c;
  70.  
  71.                             /* Stop before the buffer can overflow. */
  72.  
  73.                         if(BaudCount == sizeof(BaudBuffer) - 1)
  74.                             GotIt = TRUE;
  75.                     }
  76.                 }
  77.  
  78.                     /* Did we get the baud rate we were looking for? */
  79.  
  80.                 if(GotIt)
  81.                 {
  82.                         /* We made a connection. */
  83.  
  84.                     FlowInfo.Connect = TRUE;
  85.                     FlowInfo.Changed = TRUE;
  86.  
  87.                         /* We are no longer looking for a baud rate. */
  88.  
  89.                     BaudPending = FALSE;
  90.  
  91.                         /* Strip trailing spaces. */
  92.  
  93.                     while(BaudCount > 0 && BaudBuffer[BaudCount - 1] == ' ')
  94.                         BaudCount--;
  95.  
  96.                     BaudBuffer[BaudCount] = 0;
  97.  
  98.                         /* Turn the modem response string into a number
  99.                          * and store it as the connection speed.
  100.                          */
  101.  
  102.                     DTERate = GetBaudRate(BaudBuffer);
  103.  
  104.                         /* If no output should show up, stop
  105.                          * the data from flowing until it gets
  106.                          * enabled again. This will usually happen
  107.                          * after the dial panel has closed and
  108.                          * reopened the display.
  109.                          */
  110.  
  111.                     if(ConsoleQuiet)
  112.                     {
  113.                             /* Keep the current data. */
  114.  
  115.                         DataHold = Data;
  116.  
  117.                             /* If there is any data left,
  118.                              * remember how much there is.
  119.                              * If nothing is left, don't
  120.                              * bother. The cache read routines
  121.                              * will check if DataHold is not NULL
  122.                              * and if DataSize is greater than
  123.                              * zero before accessing the cache.
  124.                              */
  125.  
  126.                         if(Size > 0)
  127.                             DataSize = Size - 1;
  128.                         else
  129.                             DataSize = 0;
  130.  
  131.                         UpdateSerialJob();
  132.  
  133.                         OriginalSize -= DataSize;
  134.                     }
  135.                 }
  136.             }
  137.             else
  138.             {
  139.                 BOOL MatchMade;
  140.                 LONG i;
  141.  
  142.                 do
  143.                 {
  144.                     MatchMade = FALSE;
  145.  
  146.                         /* Scan all ID strings for matches. */
  147.  
  148.                     for(i = ScanStart ; i <= ScanEnd ; i++)
  149.                     {
  150.                             /* This sequence is a likely
  151.                              * match.
  152.                              */
  153.  
  154.                         if(AttentionCount[i] == FlowCount)
  155.                         {
  156.                                 /* Does the character
  157.                                  * fit into the sequence?
  158.                                  */
  159.  
  160.                             if(c == AttentionBuffers[i][FlowCount] & Mask)
  161.                             {
  162.                                 MatchMade = TRUE;
  163.  
  164.                                     /* Did we hit the
  165.                                      * last character
  166.                                      * in the sequence?
  167.                                      */
  168.  
  169.                                 if(++AttentionCount[i] == AttentionLength[i])
  170.                                 {
  171.                                         /* We've got a valid
  172.                                          * sequence, now look
  173.                                          * which flags to change.
  174.                                          */
  175.  
  176.                                     switch(i)
  177.                                     {
  178.                                             /* We got a `no carrier' message. */
  179.  
  180.                                         case SCAN_NOCARRIER:
  181.  
  182.                                             if(!FlowInfo.NoCarrier)
  183.                                             {
  184.                                                 FlowInfo.NoCarrier    = TRUE;
  185.                                                 FlowInfo.Changed    = TRUE;
  186.                                             }
  187.  
  188.                                             break;
  189.  
  190.                                             /* Got another call. */
  191.  
  192.                                         case SCAN_RING:
  193.  
  194.                                             if(!FlowInfo.Ring)
  195.                                             {
  196.                                                 FlowInfo.Ring        = TRUE;
  197.                                                 FlowInfo.Changed    = TRUE;
  198.                                             }
  199.  
  200.                                             break;
  201.  
  202.                                             /* Got a voice call. */
  203.  
  204.                                         case SCAN_VOICE:
  205.  
  206.                                             if(!FlowInfo.Voice)
  207.                                             {
  208.                                                 FlowInfo.Voice        = TRUE;
  209.                                                 FlowInfo.Changed    = TRUE;
  210.                                             }
  211.  
  212.                                             break;
  213.  
  214.                                             /* Modem reported an error. */
  215.  
  216.                                         case SCAN_ERROR:
  217.  
  218.                                             if(!FlowInfo.Error)
  219.                                             {
  220.                                                 FlowInfo.Error        = TRUE;
  221.                                                 FlowInfo.Changed    = TRUE;
  222.                                             }
  223.  
  224.                                             break;
  225.  
  226.                                             /* Got a connect message. */
  227.  
  228.                                         case SCAN_CONNECT:
  229.  
  230.                                             if(!Online)
  231.                                             {
  232.                                                 BaudBuffer[0]    = 0;
  233.  
  234.                                                 BaudPending    = TRUE;
  235.                                                 BaudCount    = 0;
  236.                                             }
  237.  
  238.                                             break;
  239.  
  240.                                             /* Line is busy. */
  241.  
  242.                                         case SCAN_BUSY:
  243.  
  244.                                             if(!FlowInfo.Busy)
  245.                                             {
  246.                                                 FlowInfo.Busy        = TRUE;
  247.                                                 FlowInfo.Changed    = TRUE;
  248.                                             }
  249.  
  250.                                             break;
  251.  
  252.                                         case SCAN_NODIALTONE:
  253.  
  254.                                             if(!FlowInfo.NoDialTone)
  255.                                             {
  256.                                                 FlowInfo.NoDialTone    = TRUE;
  257.                                                 FlowInfo.Changed    = TRUE;
  258.                                             }
  259.  
  260.                                             break;
  261.  
  262.                                             /* Modem accepted a command. */
  263.  
  264.                                         case SCAN_OK:
  265.  
  266.                                             if(!FlowInfo.Ok)
  267.                                             {
  268.                                                 FlowInfo.Ok            = TRUE;
  269.                                                 FlowInfo.Changed    = TRUE;
  270.                                             }
  271.  
  272.                                             break;
  273.  
  274.                                         default:
  275.  
  276.                                             if(!FlowInfo.Signature)
  277.                                             {
  278.                                                 FlowInfo.Signature    = i;
  279.                                                 FlowInfo.Changed    = TRUE;
  280.                                             }
  281.  
  282.                                             break;
  283.                                     }
  284.                                 }
  285.                             }
  286.                         }
  287.                     }
  288.  
  289.                     if(MatchMade)
  290.                         FlowCount++;
  291.                     else
  292.                     {
  293.                         if(FlowCount)
  294.                         {
  295.                             FlowCount = 0;
  296.  
  297.                             memset(AttentionCount,0,sizeof(AttentionCount));
  298.                         }
  299.                         else
  300.                             break;
  301.                     }
  302.                 }
  303.                 while(!FlowCount);
  304.             }
  305.         }
  306.     }
  307.     while(--Size);
  308.  
  309.     return(OriginalSize);
  310. }
  311.  
  312.     /* ResetDataFlowFilter():
  313.      *
  314.      *    Set up the data flow parser. The parser scans the serial
  315.      *    output data for more or less interesting modem output
  316.      *    (carrier lost, connect, etc.).
  317.      */
  318.  
  319. VOID
  320. ResetDataFlowFilter()
  321. {
  322.     LONG i;
  323.  
  324.     for(i = 0 ; i < SCAN_COUNT ; i++)
  325.         AttentionBuffers[i][0] = 0;
  326.  
  327.         /* Set up `NO CARRIER' message. */
  328.  
  329.     if(Config->ModemConfig->NoCarrier[0])
  330.         LimitedSPrintf(ATTENTION_BUFFER_SIZE,AttentionBuffers[SCAN_NOCARRIER],"%s\r",Config->ModemConfig->NoCarrier);
  331.  
  332.         /* Set up `CONNECT' message. */
  333.  
  334.     LimitedStrcpy(ATTENTION_BUFFER_SIZE,AttentionBuffers[SCAN_CONNECT],Config->ModemConfig->Connect);
  335.  
  336.         /* Set up `VOICE' message. */
  337.  
  338.     if(Config->ModemConfig->Voice[0])
  339.         LimitedSPrintf(ATTENTION_BUFFER_SIZE,AttentionBuffers[SCAN_VOICE],"%s\r",Config->ModemConfig->Voice);
  340.  
  341.         /* Set up `RING' message. */
  342.  
  343.     if(Config->ModemConfig->Ring[0])
  344.         LimitedSPrintf(ATTENTION_BUFFER_SIZE,AttentionBuffers[SCAN_RING],"%s\r",Config->ModemConfig->Ring);
  345.  
  346.         /* Set up `BUSY' message. */
  347.  
  348.     if(Config->ModemConfig->Busy[0])
  349.         LimitedSPrintf(ATTENTION_BUFFER_SIZE,AttentionBuffers[SCAN_BUSY],"%s\r",Config->ModemConfig->Busy);
  350.  
  351.         /* Set up `NO DIALTONE' message. */
  352.  
  353.     if(Config->ModemConfig->NoDialTone[0])
  354.         LimitedSPrintf(ATTENTION_BUFFER_SIZE,AttentionBuffers[SCAN_NODIALTONE],"%s\r",Config->ModemConfig->NoDialTone);
  355.  
  356.         /* Set up `OK' message. */
  357.  
  358.     if(Config->ModemConfig->Ok[0])
  359.         LimitedSPrintf(ATTENTION_BUFFER_SIZE,AttentionBuffers[SCAN_OK],"%s\r",Config->ModemConfig->Ok);
  360.  
  361.         /* Set up `ERROR' message. */
  362.  
  363.     if(Config->ModemConfig->Error[0])
  364.         LimitedSPrintf(ATTENTION_BUFFER_SIZE,AttentionBuffers[SCAN_ERROR],"%s\r",Config->ModemConfig->Error);
  365.  
  366.         /* Reset match counter. */
  367.  
  368.     FlowCount = 0;
  369.  
  370.         /* Reset indices. */
  371.  
  372.     memset(AttentionCount,0,sizeof(AttentionCount));
  373.  
  374.         /* Determine lengths. */
  375.  
  376.     for(i = 0 ; i < SCAN_COUNT ; i++)
  377.         AttentionLength[i] = strlen(AttentionBuffers[i]);
  378.  
  379.     for(i = SCAN_SIGDEFAULTUPLOAD ; i <= SCAN_SIGBINARYDOWNLOAD ; i++)
  380.     {
  381.         if(AttentionLength[i] = Config->TransferConfig->Signatures[TRANSFERSIG_DEFAULTUPLOAD + i - SCAN_SIGDEFAULTUPLOAD].Length)
  382.             CopyMem(Config->TransferConfig->Signatures[TRANSFERSIG_DEFAULTUPLOAD + i - SCAN_SIGDEFAULTUPLOAD].Signature,AttentionBuffers[i],Config->TransferConfig->Signatures[TRANSFERSIG_DEFAULTUPLOAD + i - SCAN_SIGDEFAULTUPLOAD].Length);
  383.     }
  384.  
  385.     if(Config->TransferConfig->ASCIIUploadType != XFER_XPR && Config->TransferConfig->ASCIIUploadType != XFER_EXTERNALPROGRAM)
  386.         AttentionLength[SCAN_SIGASCIIUPLOAD] = 0;
  387.  
  388.     if(Config->TransferConfig->ASCIIDownloadType != XFER_XPR && Config->TransferConfig->ASCIIUploadType != XFER_EXTERNALPROGRAM)
  389.         AttentionLength[SCAN_SIGASCIIDOWNLOAD] = 0;
  390.  
  391.     if(Config->TransferConfig->TextUploadType != XFER_XPR && Config->TransferConfig->TextUploadType != XFER_EXTERNALPROGRAM)
  392.         AttentionLength[SCAN_SIGTEXTUPLOAD] = 0;
  393.  
  394.     if(Config->TransferConfig->TextDownloadType != XFER_XPR && Config->TransferConfig->TextUploadType != XFER_EXTERNALPROGRAM)
  395.         AttentionLength[SCAN_SIGTEXTDOWNLOAD] = 0;
  396.  
  397.     if(Config->TransferConfig->BinaryUploadType != XFER_XPR && Config->TransferConfig->BinaryUploadType != XFER_EXTERNALPROGRAM)
  398.         AttentionLength[SCAN_SIGBINARYUPLOAD] = 0;
  399.  
  400.     if(Config->TransferConfig->BinaryDownloadType != XFER_XPR && Config->TransferConfig->BinaryUploadType != XFER_EXTERNALPROGRAM)
  401.         AttentionLength[SCAN_SIGBINARYDOWNLOAD] = 0;
  402.  
  403.         /* No, we are not yet looking for a baud rate. */
  404.  
  405.     BaudPending = FALSE;
  406.  
  407.         /* Reset the flags. */
  408.  
  409.     memset(&FlowInfo,FALSE,sizeof(struct FlowInfo));
  410.  
  411.         /* Full data check is slower than looking for
  412.          * just a single sequence (such as the `CONNECT'
  413.          * below). This mode is reserved for the dial panel.
  414.          */
  415.  
  416.     ScanStart    = 0;
  417.     ScanEnd        = SCAN_COUNT - 1;
  418.     UseFlow        = TRUE;
  419. }
  420.